package com.ustcinfo.study.mr.yangxinxin;

import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.w3c.dom.*;

import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.Text;
import java.io.IOException;

public class YangxinxinWordCount {
    private static class MyMapper extends Mapper<Object, Text, Text, IntWritable> {
        private static final IntWritable count = new IntWritable(1);

        @Override
        protected void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            String[] arr = line.split(" ");
            for (int i = 0; i < arr.length; i++) {
                Text word = new Text() {
                    public boolean isComment() {
                        return false;
                    }

                    public String getValue() {
                        return null;
                    }

                    public void setValue(String value) {

                    }

                    public void setParentElement(SOAPElement parent) throws SOAPException {

                    }

                    public SOAPElement getParentElement() {
                        return null;
                    }

                    public void detachNode() {

                    }

                    public void recycleNode() {

                    }

                    public org.w3c.dom.Text splitText(int offset) throws DOMException {
                        return null;
                    }

                    public boolean isElementContentWhitespace() {
                        return false;
                    }

                    public String getWholeText() {
                        return null;
                    }

                    public org.w3c.dom.Text replaceWholeText(String content) throws DOMException {
                        return null;
                    }

                    public String getData() throws DOMException {
                        return null;
                    }

                    public void setData(String data) throws DOMException {

                    }

                    public int getLength() {
                        return 0;
                    }

                    public String substringData(int offset, int count) throws DOMException {
                        return null;
                    }

                    public void appendData(String arg) throws DOMException {

                    }

                    public void insertData(int offset, String arg) throws DOMException {

                    }

                    public void deleteData(int offset, int count) throws DOMException {

                    }

                    public void replaceData(int offset, int count, String arg) throws DOMException {

                    }

                    public String getNodeName() {
                        return null;
                    }

                    public String getNodeValue() throws DOMException {
                        return null;
                    }

                    public void setNodeValue(String nodeValue) throws DOMException {

                    }

                    public short getNodeType() {
                        return 0;
                    }

                    public Node getParentNode() {
                        return null;
                    }

                    public NodeList getChildNodes() {
                        return null;
                    }

                    public Node getFirstChild() {
                        return null;
                    }

                    public Node getLastChild() {
                        return null;
                    }

                    public Node getPreviousSibling() {
                        return null;
                    }

                    public Node getNextSibling() {
                        return null;
                    }

                    public NamedNodeMap getAttributes() {
                        return null;
                    }

                    public Document getOwnerDocument() {
                        return null;
                    }

                    public Node insertBefore(Node newChild, Node refChild) throws DOMException {
                        return null;
                    }

                    public Node replaceChild(Node newChild, Node oldChild) throws DOMException {
                        return null;
                    }

                    public Node removeChild(Node oldChild) throws DOMException {
                        return null;
                    }

                    public Node appendChild(Node newChild) throws DOMException {
                        return null;
                    }

                    public boolean hasChildNodes() {
                        return false;
                    }

                    public Node cloneNode(boolean deep) {
                        return null;
                    }

                    public void normalize() {

                    }

                    public boolean isSupported(String feature, String version) {
                        return false;
                    }

                    public String getNamespaceURI() {
                        return null;
                    }

                    public String getPrefix() {
                        return null;
                    }

                    public void setPrefix(String prefix) throws DOMException {

                    }

                    public String getLocalName() {
                        return null;
                    }

                    public boolean hasAttributes() {
                        return false;
                    }

                    public String getBaseURI() {
                        return null;
                    }

                    public short compareDocumentPosition(Node other) throws DOMException {
                        return 0;
                    }

                    public String getTextContent() throws DOMException {
                        return null;
                    }

                    public void setTextContent(String textContent) throws DOMException {

                    }

                    public boolean isSameNode(Node other) {
                        return false;
                    }

                    public String lookupPrefix(String namespaceURI) {
                        return null;
                    }

                    public boolean isDefaultNamespace(String namespaceURI) {
                        return false;
                    }

                    public String lookupNamespaceURI(String prefix) {
                        return null;
                    }

                    public boolean isEqualNode(Node arg) {
                        return false;
                    }

                    public Object getFeature(String feature, String version) {
                        return null;
                    }

                    public Object setUserData(String key, Object data, UserDataHandler handler) {
                        return null;
                    }

                    public Object getUserData(String key) {
                        return null;
                    }
                };
                word.setValue(arr[i]);
                context.write(word, count);
            }
        }
    }

    private static class MyReducer extends Reducer<Text, IntWritable, Text, IntWritable> {
        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            int sum = 0;
            for (IntWritable val : values) {
                sum += val.get();
            }
            context.write(key, new IntWritable(sum));
        }
    }

    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Job job = Job.getInstance();
        job.setJobName("yxx_word_count");
        job.setJarByClass(YangxinxinWordCount.class);
        job.setMapperClass(MyMapper.class);
        job.setCombinerClass(MyReducer.class);
        job.setReducerClass(MyReducer.class);
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(IntWritable.class);
        org.apache.hadoop.mapreduce.lib.input.FileInputFormat.addInputPath(job, new Path(args[0]));
        org.apache.hadoop.mapreduce.lib.output.FileOutputFormat.setOutputPath(job, new Path(args[1]));
        int status = job.waitForCompletion(true) ? 0 : 1;
        System.exit(status);
    }
}